<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
class PcateController extends \App\Http\Controllers\AdminController{
    /**
     * 产品列表
     * @return type
     */
    public function index() {
        $pcates = \App\Models\Pcate::orderBy('sort', 'asc')->paginate();
        $assign = compact('pcates');
        return view('admin.pcate.index', $assign);
    }
    /**
     * 修改/删除产品
     * @param Request $request
     * @param type $id
     * @param type $status
     * @return type
     */
    public function edit(Request $request, $id, $status=false) {
        $pcate = \App\Models\Pcate::find($id);
        if (!$pcate) {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 0,
                'info'      => '商品不存在'
            ]);
        }
        if ($status === '0') {
            $pcate->delete();
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '删除成功'
            ]);
        }
        if ($request->ajax()) {
            $pcate->name = $request->input('name');
            $pcate->sort = $request->input('sort');
            $pcate->rate = $request->input('rate');
            $pcate->save();
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '修改成功'
            ]);
        }
        $assign = compact('pcate');
        return view('admin.pcate.edit', $assign);
    }
    /**
     * 添加新产品
     */
    public function add(Request $request) {
        if ($request->ajax()) {
            return \App\Models\Pcate::addPcate($request);
        }
        return view('admin.pcate.add');
    }
}
